/*
Package db :
Copyright Huawei Corp. All Rights Reserved.
*/
package db

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"nft-server/config"
	"nft-server/logger"
)

var Factory *DBFactory

type DBFactory struct {
}

type DB interface {
	Init(c *config.DBConfig) error
}

func GetDBFactory() *DBFactory {
	if Factory == nil {
		Factory = &DBFactory{}
	}
	return Factory
}

func (f *DBFactory) InitDBServer(sqlDriverType string, config *config.DBConfig) error {
	logger.Info("model factory initialize for sql driver [%v]", sqlDriverType)
	// 统一更新beego orm默认配置Limit为-1（不限制），避免QuerySeter.All()调用默认1000导致部分场景无法获取完整数据
	orm.DefaultRowsLimit = DefaultRowsLimit
	switch sqlDriverType {
	case GaussSQLDriver:
		gdb := &GuassDB{}
		if err := gdb.Init(config); err != nil {
			return err
		}
	default:
		return fmt.Errorf("unsupported sql driver: %v", sqlDriverType)
	}
	return nil
}
